package org.hgvc.webapp.ets.jsf.beans.mastersetup;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Race;
import org.hgvc.webapp.ets.bo.Salutation;
import org.hgvc.webapp.ets.constant.AdminConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;


public class SalutationBean extends AbstractBean{
	public static final String BEAN_NAME="salutationBean";
	
	private MasterSetupService masterSetupService;
	private List<Salutation> salutationList;
	private Salutation salutation;
	private boolean catchError;
	public List<Salutation> getSalutationList() {
		return salutationList;
	}
	public void setSalutationList(List<Salutation> salutationList) {
		this.salutationList = salutationList;
	}
	public Salutation getSalutation() {
		return salutation;
	}
	public void setSalutation(Salutation salutation) {
		this.salutation = salutation;
	}
	public void setMasterSetupService(MasterSetupService masterSetupService) {
		this.masterSetupService = masterSetupService;
	}
	
	public void init(){
		try{
			salutationList = masterSetupService.getSalutationList();	
		}catch(BaseException be){
			initErrorBean(be);
		}
	}
	
	public String navAddAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		outcome=outcome.concat("_"+getRoleName());
		salutation=new Salutation();
		return outcome;
	}
	public String addAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		outcome=outcome.concat("_"+getRoleName());
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void addActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.salutation.setCreatedBy(getThisUser());
			this.salutation.setCreatedDate(new Date());
			this.salutation.setIsDeleted(CommonConstant.NOT_DELETED);
			this.masterSetupService.saveSalutation(this.salutation);
			this.salutationList = this.masterSetupService.getSalutationList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String navEditAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		outcome=outcome.concat("_"+getRoleName());
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void navEditActionEvent(ActionEvent event){
		this.catchError=false;
		UIParameter param = (UIParameter) event.getComponent().findComponent("salutationId");
		String salutationId = (String)param.getValue();
		for(Salutation sa :salutationList){
			if(salutationId.equalsIgnoreCase(sa.getSalutationId())){
				this.salutation = sa;
				break;
			}
		}
	}
	
	public String updateAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		outcome=outcome.concat("_"+getRoleName());
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void updateActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.salutation.setModifiedBy(getThisUser());
			this.salutation.setModifiedDate(new Date());
			this.masterSetupService.updateSalutation(this.salutation);
			this.salutationList = this.masterSetupService.getSalutationList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String deleteAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		outcome=outcome.concat("_"+getRoleName());
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void deleteActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			List<Salutation> deleteList = new ArrayList<Salutation>();
			for (Salutation salutation : this.salutationList){
				if (salutation.isMarkAsAction())
					deleteList.add(salutation);
			}
			this.masterSetupService.deleteSalutation(deleteList);
			this.salutationList = this.masterSetupService.getSalutationList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String backToListAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		outcome=outcome.concat("_"+getRoleName());
		return outcome;
	}
}
